package tau.dimes.visualization.applet.heatMap;

import java.util.HashMap;
import java.util.Map;

import tau.dimes.visualization.applet.Visualization;
import tau.dimes.visualization.applet.data.Edge;
import tau.dimes.visualization.applet.data.Hop;
import tau.dimes.visualization.applet.data.Trace;

/**
 * Implements the heat map visualization.
 */
public class HeatMap extends Visualization
{
	private final Map<String, HeatMapNode> nodes;
	private float maxHeat;

	/**
	 * Constructs a new HeatMap.
	 */
	public HeatMap()
	{
		nodes = new HashMap<String, HeatMapNode>();
	}
	
	@Override
	public void draw(int timeSlot, float alpha)
	{
		maxHeat = 1;
		
		for (HeatMapNode node : nodes.values())
			node.calcHeat(timeSlot, alpha);
		
		for (HeatMapNode node : nodes.values())
			node.draw();
	}

	@Override
	public void init()
	{
		for (Hop hop : traceDataSet.getHops().values())
			nodes.put(hop.getId(), new HeatMapNode(applet, this, applet.mapHAngle(hop
				.getLongitude()), applet.mapVAngle(hop.getLatitude())));
		
		int timeSlot;
		for (Trace trace : traceDataSet.getTraces())
		{
			timeSlot = 1;
			for (Edge edge : trace)
			{
				if (timeSlot == 1)
					nodes.get(edge.getSource().getId()).incrementHeat(1);
				
				nodes.get(edge.getDestination().getId()).incrementHeat(timeSlot + 1);
				
				timeSlot++;
			}
		}
	}

	@Override
	public int getTimeSlots()
	{
		return traceDataSet.getMaxEdges() + 1;
	}
	
	/**
	 * Gets the maximum heat value.
	 * @return Maximum heat value.
	 */
	float getMaxHeat()
	{
		return maxHeat;
	}
	
	/**
	 * Sets the maximum heat value.
	 * @param heat Maximum heat value.
	 */
	void setMaxHeat(float heat)
	{
		this.maxHeat = Math.max(this.maxHeat, heat);
	}
}
